# -*- coding: utf-8 -*-
"""
Tencent is pleased to support the open source community by making 蓝鲸智云PaaS平台社区版 (BlueKing PaaS Community
Edition) available.
...
"""

from blueapps.conf.default_settings import *  # noqa
from blueapps.conf.log import get_logging_config_dict
import os

# ========== APP 配置 ==========
# 保留默认 INSTALLED_APPS，并添加你需要的
INSTALLED_APPS += (  # noqa
    "corsheaders",              # 解决跨域
    "home_application",
    "mako_application",
)

# ========== 中间件 MIDDLEWARE 正确做法：基于默认值追加 ==========
# 不要直接覆盖！使用元组拼接
MIDDLEWARE = (
    "corsheaders.middleware.CorsMiddleware",  # 尽量靠前
) + MIDDLEWARE  # 继承 blueapps 默认中间件
MIDDLEWARE += ("core.middleware.RecordUserBehaviorMiddleware",)
# ========== CORS 配置 ==========
CORS_ALLOW_CREDENTIALS = True  # 允许携带 cookie

# ✅ 修复：只定义一次，支持多个域名 + 环境变量
CORS_ALLOWED_ORIGINS = [
    "http://dev.ce.bktencent.com:5000",
    "http://127.0.0.1:8080",
]

# 可选：通过环境变量扩展
_extra_origin = os.getenv("CORS_ALLOWED_HOSTS")
if _extra_origin:
    CORS_ALLOWED_ORIGINS.append(_extra_origin)

# ========== 其他配置保持不变 ==========
DEFAULT_AUTO_FIELD = "django.db.models.AutoField"
STATIC_VERSION = "1.0"
STATICFILES_DIRS = [os.path.join(BASE_DIR, "static")]  # noqa
IS_USE_CELERY = False
FRONTEND_BACKEND_SEPARATION = False
CELERYD_CONCURRENCY = os.getenv("BK_CELERYD_CONCURRENCY", 2)
CELERY_IMPORTS = ()
LOG_LEVEL = "INFO"
LOGGING = get_logging_config_dict(locals())
INIT_SUPERUSER = []
MAKO_DEFAULT_FILTERS = ["h"]
IS_BKUI_HISTORY_MODE = False
IS_AJAX_PLAIN_MODE = False
LOCALE_PATHS = (os.path.join(BASE_DIR, "locale"),)  # noqa
USE_TZ = True
TIME_ZONE = "Asia/Shanghai"
LANGUAGE_CODE = "zh-hans"
LANGUAGES = (
    ("en", u"English"),
    ("zh-hans", u"简体中文"),
)

"""
以下为框架代码 请勿修改
"""
# celery settings
if IS_USE_CELERY:
    INSTALLED_APPS = locals().get("INSTALLED_APPS", [])
    INSTALLED_APPS += ("django_celery_beat", "django_celery_results")
    CELERY_ENABLE_UTC = False
    CELERYBEAT_SCHEDULER = "django_celery_beat.schedulers.DatabaseScheduler"

# remove disabled apps
if locals().get("DISABLED_APPS"):
    INSTALLED_APPS = locals().get("INSTALLED_APPS", [])
    DISABLED_APPS = locals().get("DISABLED_APPS", [])
    INSTALLED_APPS = [_app for _app in INSTALLED_APPS if _app not in DISABLED_APPS]
    # ... 其他清理逻辑